import ajax from './ajax'
import { SessionStorage } from 'quasar'

function collectTags( data ){
  let allTags = []
  let allTagsMap = []
  data.map( account => {
      let { tags } = account
      tags.map( item => {
          let [key, count] = item.split('|')
          if( !key ) {
              return
          }
          // let _key = 't_' + key.charCodeAt()
          if( allTagsMap.includes(key) ){
              let _index = allTagsMap.indexOf(key)
              allTags[_index].count += Number(count)
          } else {
              allTagsMap.push( key )
              allTags.push({
                  key,
                  count: Number(count)
              })
          }
      })
  })
  
  SessionStorage.set( 'tags', allTags )
}

export default {
  loadCategories (onSuccess) {
    if( SessionStorage.has( 'categories' )){
      onSuccess( SessionStorage.get.item( 'categories' ))
      return 
    }

    ajax.get( '/getCategories' )
      .then( result => {
        let { data, ret } = result.data
        // console.log( 'data::::', data ) 
        if( ret == 0 ){
          onSuccess( data )
          SessionStorage.set( 'categories', data )

        }
    });
  },
  loadAccountByCategory( onSuccess ){
      // if( SessionStorage.has( 'accounts' )){
      //     onSuccess( SessionStorage.get.item( 'accounts' ))
      //     return 
      // }
      ajax.get( '/getAccounts' )
        .then( result => {
          let { data, ret } = result.data
          // console.log( 'data::::', data ) 
          if( ret == 0 ){
            onSuccess( data )
            // collectTags( data )
            SessionStorage.set( 'accounts', data )
          }
      });

      // onSuccess( result )
  },
  deleteAccount( accountid, onSuccess ){
    ajax.post( `/offical/${accountid}/delete` )
        .then(onSuccess);
    // onSuccess()
  },
  loadArticals( accountid, page, onSuccess ) {
    
    ajax.get( `/offical/${accountid}/articals?page=${page}&limit=100` )
      .then( result => {
        let { data, ret } = result.data
        // console.log( 'data::::', data ) 
        if( ret == 0 ){
          onSuccess( data )
        }
    });
  },
  loadCategoryArticals( key, page, limit, onSuccess ){
    ajax.get( `/offical/articals?category=${key}&page=${page}&limit=${limit}` )
      .then( result => {
        let { data, ret } = result.data
        // console.log( 'data::::', data ) 
        if( ret == 0 ){
          onSuccess( data )
        }
    });
  },
  updateArticals( articalId, options, onSuccess ){
    ajax.post(`/artical/${articalId}/update`, options )
    .then( res => {
      let data = res.data;
      if(onSuccess) {
        onSuccess(data); 
      }
    }); 
  },
  deleteArtical( articalId, onSuccess ){
    ajax.post(`/artical/delete/${articalId}` )
    .then( res => {
      let data = res.data;
      if(onSuccess) {
        onSuccess(data); 
      }
    })
  },
  loadAllTags( onSuccess ){
    ajax.get(`/getTags` )
    .then( res => {
      let result = res.data
      let { ret, data } = result
      if( ret == 0 ){
        !!onSuccess && onSuccess(data); 
        SessionStorage.set( 'tags', data )
      }
    }); 
  },
  updateTags( type, tags, onSuccess ){
    ajax.post(`/offical/tags/update`, { type, tags } )
    .then( res => {
      let result = res.data
      if( result.ret == 0 ){
        !!onSuccess && onSuccess(); 
      } else {

      }
    }); 
  }
}